package com.liao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.bean.Role;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.dao.RoleDao;
import com.liao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult<Role> queryRoleListByPage(Integer currentPage, Integer pageSize, String roleName) throws Exception {

        PageHelper.startPage(currentPage,pageSize);
        List<Role> roleList = roleDao.selectRoleListLikeRoleName(roleName);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        return new PageResult<>(0, Msg.ROLE_QUERY_SUCCESS,pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void addRole(Role role) throws Exception {
        roleDao.addRole(role);
    }

    @Override
    public Role queryRoleById(String roleId) throws Exception {
        return roleDao.selectRoleById(roleId);
    }

    @Override
    public void deleteRole(String roleId) throws Exception {
        roleDao.deleteRole(roleId);
    }

    @Override
    public void editRole(String oldRoleId, String roleId, String roleName, String roleDesc) throws Exception {
        HashMap<String,Object> map = new HashMap<>();
        map.put("oldRoleId",oldRoleId);
        map.put("roleId",roleId);
        map.put("roleName",roleName);
        map.put("roleDesc",roleDesc);
        roleDao.updateRole(map);
    }
}
